package com.dodoke.ojserver.controller;

import com.dodoke.ojserver.bean.Problem;
import com.dodoke.ojserver.bean.dto.ProblemAddDTO;
import com.dodoke.ojserver.bean.mapper.ProblemMapStruct;
import com.dodoke.ojserver.bean.valid.ProblemValidGroups;
import com.dodoke.ojserver.bean.vo.ProblemVO;
import com.dodoke.ojserver.common.Difficulty;
import com.dodoke.ojserver.config.Result;
import com.dodoke.ojserver.service.IProblemService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/problem")
@Api(tags = "问题接口")
public class ProblemController {

    @Autowired
    private IProblemService problemService;

    @GetMapping("/get/{problemId}")
    @ApiOperation(value="通过编号查询问题")
    @ApiImplicitParam(name = "problemId", value = "问题编号", required = true, dataType = "int")
    public Result get(@PathVariable int problemId) {
        Problem problem = problemService.getProblemById(problemId);
        return Result.success("查询成功", ProblemMapStruct.PROBLEM_MAPPER.problemToProblemVO(problem));
    }

    @GetMapping("/list")
    @ApiOperation(value="通过分页查询已发布的问题列表", notes = "默认根据 问题编号 从低到高 排序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "显示条数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "difficulty", value = "难度分类", required = false, dataType = "Difficulty"),
            @ApiImplicitParam(name = "title", value = "模糊查询", required = false, dataType = "String"),
            @ApiImplicitParam(name = "reverse", value = "是否 从高到低 排序", required = false, dataType = "Boolean"),
            @ApiImplicitParam(name = "passRate", value = "是否根据 通过率 排序", required = false, dataType = "Boolean")
    })
    public Result list(
            @RequestParam(value = "pageNum", defaultValue = "0") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "difficulty", defaultValue = "") Difficulty difficulty,
            @RequestParam(value = "title", defaultValue = "") String title,
            @RequestParam(value = "reverse", defaultValue = "false") Boolean reverse,
            @RequestParam(value = "passRate", defaultValue = "false") Boolean passRate
    ) {
        Problem problem = new Problem();
        // 设置状态为发布
        problem.setStatus(1);
        // 设置难度分类
        problem.setDifficulty(difficulty);
        // 设置模糊查询标题
        if (!"".equals(title)){
            problem.setTitle(title);
        }
        // 设置排序方式
        String columnOrder = "problem_id";
        String reverseOrder = "asc";
        if (reverse){
            reverseOrder = "desc";
        }
        if (passRate){
            columnOrder = "solved";
        }
        String orderBy = columnOrder + " " +reverseOrder;
        // 调用查询方法
        PageInfo<ProblemVO> problemVOPageInfo = problemService.listProblem(problem, pageNum, pageSize, orderBy);
        return Result.success("查询成功", problemVOPageInfo);
    }

    @GetMapping("/rand")
    @ApiOperation(value="随机抽取问题编号")
    public Result rand() {
        Problem problem = problemService.getProblemRand();
        Integer problemId = problem.getProblemId();
        return Result.success("随机抽取问题编号成功", problemId);
    }

    @PostMapping("/add")
    @ApiOperation(value="新增问题")
    @ApiImplicitParam(name = "problemAddDTO", value = "问题新增数据传输对象", required = true, dataType = "ProblemAddDTO")
    public Result add(@RequestBody @Validated(ProblemValidGroups.Add.class) ProblemAddDTO problemAddDTO) {
        int i = problemService.addProblem(problemAddDTO);
        if (i != 1){
            return Result.error(500, "新增失败");
        }
        return Result.success("新增成功", null);
    }

    @PutMapping("/edit")
    @ApiOperation(value="通过编号更新问题")
    @ApiImplicitParam(name = "problemAddDTO", value = "问题新增数据传输对象", required = true, dataType = "ProblemAddDTO")
    public Result edit(@RequestBody @Validated(ProblemValidGroups.Edit.class) ProblemAddDTO problemAddDTO) {
        int i = problemService.editProblem(problemAddDTO);
        if (i != 1){
            return Result.error(500, "更新失败");
        }
        return Result.success("更新成功", null);
    }

    @ApiOperation(value="通过编号删除问题")
    @ApiImplicitParam(name = "problemIds", value = "问题id数组", required = true, dataType = "int", allowMultiple = true)
    @DeleteMapping("/delete/{problemIds}")
    public Result delete(@PathVariable int[] problemIds) {
        int i = problemService.deleteProblemList(problemIds);
        if (i != problemIds.length) {
            return Result.error(500, "删除异常");
        }
        return Result.success("删除成功", null);
    }
}
